package com.demo.wrapper;

import com.demo.wrapper.interfaces.IEsAggWrapper;
import com.demo.wrapper.interfaces.IEsLimitWrapper;
import com.demo.wrapper.pojo.Agg;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @param <AggWrapper> AbstractEsAggWrapper的子类
 * @author 19242
 */
public abstract class AbstractEsAggWrapper<AggWrapper extends AbstractEsAggWrapper<AggWrapper, Wrapper>, Wrapper extends IEsLimitWrapper<Wrapper, AggWrapper>> implements IEsAggWrapper<AggWrapper, Wrapper> {

    private final AggWrapper child = (AggWrapper) this;
    private Agg agg;
    private List<AbstractEsAggWrapper<AggWrapper, Wrapper>> aggChildren;

    List<AbstractEsAggWrapper<AggWrapper, Wrapper>> getAggChildren(boolean instance) {
        if (instance && Objects.isNull(aggChildren)) {
            aggChildren = new ArrayList<>(5);
        }
        return aggChildren;
    }

    @Override
    public AggWrapper setAgg(Agg agg) {
        this.agg = agg;
        return child;
    }

    protected Agg getAgg(){
        return agg;
    }

    @Override
    public AggWrapper addAggChildren(AggWrapper child) {
        getAggChildren(true).add(child);
        return child;
    }

    @Override
    public AggWrapper reset() {
        if (Objects.isNull(aggChildren) || aggChildren.isEmpty()) {
            aggChildren.clear();
        }
        agg = null;
        return child;
    }
}
